package handler

import (
	"chat/api/requset"
	"chat/consts"
	"chat/internal/dao"
	"chat/internal/model"
	"chat/pkg"
	"fmt"
	"github.com/dgrijalva/jwt-go"
	"github.com/gin-gonic/gin"
	"github.com/share309/gojwt"
	"log"
	"net/http"
	"time"
)

func Register(c *gin.Context) {
	var req requset.RegisterReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	var user model.User
	dao.GetOneByFields(&model.User{Mobile: req.Mobile}, &user)
	if user.Id > 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "用户已存在",
			"data": nil,
		})
		return
	}
	result := dao.Create(&model.User{
		Mobile:   req.Mobile,
		Password: pkg.Md5(req.Password),
		UType:    req.UType,
	})
	if !result {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "注册失败",
			"data": false,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "注册成功",
		"data": true,
	})
}

func Login(c *gin.Context) {
	var req requset.LoginReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	var user model.User
	dao.GetOneByFields(&model.User{
		Mobile:   req.Mobile,
		Password: pkg.Md5(req.Password),
		UType:    req.UType,
	}, &user)
	if user.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "用户不存在",
			"data": nil,
		})
		return
	}
	token, err := gojwt.NewJWT(consts.JWT_KEY).CreateToken(gojwt.CustomClaims{
		ID: user.Id,
		StandardClaims: jwt.StandardClaims{
			ExpiresAt: time.Now().Unix() + 86400*7,
		},
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "生成token失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "登录成功",
		"data": token,
	})
}

func Page(c *gin.Context) {
	var req requset.PageReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	var list []model.User
	pageDto := dao.PageDto{
		Page:     req.Page,
		PageSize: req.PageSize,
	}
	paginate, err := dao.Paginate(&model.User{
		Name:  req.Name,
		State: req.State,
		Img:   req.Img,
	}, pageDto, &list)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "展示失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "展示成功",
		"data": paginate,
	})
}

func Upload(c *gin.Context) {
	file, _ := c.FormFile("file")
	log.Println(file.Filename)

	dst := "D:\\gowork\\src\\chat\\static\\upload\\" + file.Filename
	// 上传文件至指定的完整文件路径
	_ = c.SaveUploadedFile(file, dst+file.Filename)
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "图片上传成功",
		"data": fmt.Sprintf("http://localhost:8080/api/user/upload/" + file.Filename),
	})

}

func List(c *gin.Context) {
	var req requset.ListReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	var list []model.User
	dao.GetListByFields(&model.User{
		Name:  req.Name,
		State: req.State,
		Img:   req.Img,
	}, &list)
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "展示成功",
		"data": list,
	})
}
